package com.sicau.rag;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.rag.Query;
import org.springframework.ai.rag.preretrieval.query.transformation.QueryTransformer;
import org.springframework.ai.rag.preretrieval.query.transformation.RewriteQueryTransformer;
import org.springframework.stereotype.Component;

/**
 * 查询重写器
 */
@Component
public class RewriteUserMessageQueryTransformer {

    private final QueryTransformer queryTransformer;

    public RewriteUserMessageQueryTransformer(ChatModel chatModel) {
        queryTransformer = RewriteQueryTransformer.builder()
                .chatClientBuilder(ChatClient.builder(chatModel))
                .build();
    }

    /**
     * 重写用户查询
     * @param query
     * @return
     */
    public String rewrite(String query) {
        return queryTransformer.transform(new Query(query)).text();
    }

}
